//==========================================================
// 此代码由TilverNET代码生成工具自动生成，请不要修改
// 代码生成器版本号：1.2
// 作者：landy
// 联系方式：(QQ)86566178
// 创建时间：2014/8/22 11:23:39
//==========================================================

using System;
using System.Text;
using System.Data;
using System.Data.Common;
using System.Collections.Generic;
using System.Linq;
using Tilver.Data;
using Tilver.Data.DataPager;

namespace Weixin.MP.DAL.CoWeixin
{
	#region EO

	/// <summary>
	///  《dbo.天气实况(实体类)》
	/// </summary>
	[Serializable]
	public class Coder天气实况EO : IRowMapper<Coder天气实况EO>
	{
		protected long _id;
		protected string _stationnum;
		protected string _title;
		protected short? _awsType;
		protected string _text;
		protected DateTime _renewTime;


		/// <summary>
		///  《[字段]  [bigint]   自增字段》
		/// </summary>
		public long Id
		{
			get { return _id; }
			set { _id = value;}
		}

		/// <summary>
		///  《[字段]  [varchar]》
		/// </summary>
		public string Stationnum
		{
			get { return _stationnum; }
			set { _stationnum = value;}
		}

		/// <summary>
		///  《[字段]  [varchar]》
		/// </summary>
		public string Title
		{
			get { return _title; }
			set { _title = value;}
		}

		/// <summary>
		///  《[字段]  [smallint]》
		/// </summary>
		public short? AwsType
		{
			get { return _awsType; }
			set { _awsType = value;}
		}

		/// <summary>
		///  《[字段]  [ntext]》
		/// </summary>
		public string Text
		{
			get { return _text; }
			set { _text = value;}
		}

		/// <summary>
		///  《[字段]  [datetime]》
		/// </summary>
		public DateTime RenewTime
		{
			get { return _renewTime; }
			set { _renewTime = value;}
		}

		/// <summary>
		/// 默认构造函数
		/// </summary>
		public Coder天气实况EO() 
		{
			Stationnum = string.Empty;
			AwsType = (short?)0;
		}

		/// <summary>
		/// 自定义构造函数
		/// </summary>
		/// <param name = "id"></param>
		/// <param name = "stationnum"></param>
		/// <param name = "title"></param>
		/// <param name = "awsType"></param>
		/// <param name = "text"></param>
		/// <param name = "renewTime"></param>
		public Coder天气实况EO(long id, string stationnum, string title, short? awsType, string text, DateTime renewTime) 
		{
			_id = id;
			_stationnum = stationnum;
			_title = title;
			_awsType = awsType;
			_text = text;
			_renewTime = renewTime;
		}


		/// <summary>
		/// 将结果集映射成实体对象
		/// </summary>
		/// <param name = "reader">只进结果集流</param>
		/// <return></return>
		public Coder天气实况EO MapRow(IDataReader reader)
		{
			var ret = new Coder天气实况EO();
			ret.Id = (long)reader["id"];
			ret.Stationnum = (string)reader["stationnum"];
			ret.Title = (reader["title"] is DBNull) ? null : (string)reader["title"];
			ret.AwsType = (reader["aws_type"] is DBNull) ? null : (short?)reader["aws_type"];
			ret.Text = (reader["text"] is DBNull) ? null : (string)reader["text"];
			ret.RenewTime = (DateTime)reader["renewTime"];
			return ret;
		}

		/// <summary>
		/// 将结果集映射成实体对象，返回映射方法
		/// </summary>
		/// <return></return>
		public Func<IDataReader, Coder天气实况EO> GetMapper()
		{
			return MapRow;
		}


	}


	#endregion
	#region MO

	/// <summary>
	///  《dbo.天气实况(逻辑类)》
	/// </summary>
	public class Coder天气实况MO : DbManagerBase
	{

		#region 构造函数
		/// <summary>
		/// 构造函数
		/// </summary>
		/// <param name = "routeData">路由数据</param>
		public Coder天气实况MO(object routeData = null) : base(routeData) { }

		/// <summary>
		/// 构造函数
		/// </summary>
		/// <param name = "connectionStringName">连接字符串名称</param>
		public Coder天气实况MO(string connectionStringName) : base(connectionStringName) { }

		#endregion

		#region 私有方法
		/// <summary>
		/// 数据库访问操作方法
		/// </summary>
		/// <param name = "sql">SQL语句</param>
		/// <return>返回数据操作对象</return>
		private DbAdoBase AdoBase(string sql)
		{
			return DataBase.CreateDbAdo(sql, CommandType.Text);
		}

		#endregion

		#region Select方法集合

		/// <summary>
		/// 按字段查询
		/// </summary>
		/// <param name = "id"></param>
		/// <param name = "top">获取记录数，当top值小于或等于0时忽略top条件</param>
		/// <param name = "sort">排序表达式，如：field1 ASC,field2 DESC</param>
		/// <param name="tm">事务管理对象</param>
		/// <return></return>
		public List<Coder天气实况EO> SelectById(long id, int top, string sort, TransactionManager tm = null)
		{
			var sql = new StringBuilder();
			var format = new StringBuilder();
			format.Append(@"SELECT {0} [id], [stationnum], [title], [aws_type], [text], [renewTime] FROM [dbo].[天气实况] WHERE [id]=@Id");
			if (!string.IsNullOrEmpty(sort)) format.AppendFormat(" ORDER BY {0} ", sort);
			sql.AppendFormat(format.ToString(), (top > 0) ? string.Format("TOP {0}", top) : string.Empty);
			return AdoBase(sql.ToString())
							.AddInParameter("@Id", id, DbType.Int64, 8)
							.ExecuteList(new Coder天气实况EO().GetMapper(), tm);
		}
		/// <summary>
		/// 按字段查询
		/// </summary>
		/// <param name = "stationnum"></param>
		/// <param name = "top">获取记录数，当top值小于或等于0时忽略top条件</param>
		/// <param name = "sort">排序表达式，如：field1 ASC,field2 DESC</param>
		/// <param name="tm">事务管理对象</param>
		/// <return></return>
		public List<Coder天气实况EO> SelectByStationnum(string stationnum, int top, string sort, TransactionManager tm = null)
		{
			var sql = new StringBuilder();
			var format = new StringBuilder();
			format.Append(@"SELECT {0} [id], [stationnum], [title], [aws_type], [text], [renewTime] FROM [dbo].[天气实况] WHERE [stationnum]=@Stationnum");
			if (!string.IsNullOrEmpty(sort)) format.AppendFormat(" ORDER BY {0} ", sort);
			sql.AppendFormat(format.ToString(), (top > 0) ? string.Format("TOP {0}", top) : string.Empty);
			return AdoBase(sql.ToString())
							.AddInParameter("@Stationnum", stationnum, DbType.AnsiString, 16)
							.ExecuteList(new Coder天气实况EO().GetMapper(), tm);
		}
		/// <summary>
		/// 按字段查询
		/// </summary>
		/// <param name = "title"></param>
		/// <param name = "top">获取记录数，当top值小于或等于0时忽略top条件</param>
		/// <param name = "sort">排序表达式，如：field1 ASC,field2 DESC</param>
		/// <param name="tm">事务管理对象</param>
		/// <return></return>
		public List<Coder天气实况EO> SelectByTitle(string title, int top, string sort, TransactionManager tm = null)
		{
			var sql = new StringBuilder();
			var format = new StringBuilder();
			format.Append(@"SELECT {0} [id], [stationnum], [title], [aws_type], [text], [renewTime] FROM [dbo].[天气实况] WHERE [title]=@Title");
			if (!string.IsNullOrEmpty(sort)) format.AppendFormat(" ORDER BY {0} ", sort);
			sql.AppendFormat(format.ToString(), (top > 0) ? string.Format("TOP {0}", top) : string.Empty);
			return AdoBase(sql.ToString())
							.AddInParameter("@Title", title, DbType.AnsiString, 256)
							.ExecuteList(new Coder天气实况EO().GetMapper(), tm);
		}
		/// <summary>
		/// 按字段查询
		/// </summary>
		/// <param name = "awsType"></param>
		/// <param name = "top">获取记录数，当top值小于或等于0时忽略top条件</param>
		/// <param name = "sort">排序表达式，如：field1 ASC,field2 DESC</param>
		/// <param name="tm">事务管理对象</param>
		/// <return></return>
		public List<Coder天气实况EO> SelectByAwsType(short? awsType, int top, string sort, TransactionManager tm = null)
		{
			var sql = new StringBuilder();
			var format = new StringBuilder();
			format.Append(@"SELECT {0} [id], [stationnum], [title], [aws_type], [text], [renewTime] FROM [dbo].[天气实况] WHERE [aws_type]=@AwsType");
			if (!string.IsNullOrEmpty(sort)) format.AppendFormat(" ORDER BY {0} ", sort);
			sql.AppendFormat(format.ToString(), (top > 0) ? string.Format("TOP {0}", top) : string.Empty);
			return AdoBase(sql.ToString())
							.AddInParameter("@AwsType", awsType, DbType.Int16, 2)
							.ExecuteList(new Coder天气实况EO().GetMapper(), tm);
		}
		/// <summary>
		/// 按字段查询
		/// </summary>
		/// <param name = "text"></param>
		/// <param name = "top">获取记录数，当top值小于或等于0时忽略top条件</param>
		/// <param name = "sort">排序表达式，如：field1 ASC,field2 DESC</param>
		/// <param name="tm">事务管理对象</param>
		/// <return></return>
		public List<Coder天气实况EO> SelectByText(string text, int top, string sort, TransactionManager tm = null)
		{
			var sql = new StringBuilder();
			var format = new StringBuilder();
			format.Append(@"SELECT {0} [id], [stationnum], [title], [aws_type], [text], [renewTime] FROM [dbo].[天气实况] WHERE [text]=@Text");
			if (!string.IsNullOrEmpty(sort)) format.AppendFormat(" ORDER BY {0} ", sort);
			sql.AppendFormat(format.ToString(), (top > 0) ? string.Format("TOP {0}", top) : string.Empty);
			return AdoBase(sql.ToString())
							.AddInParameter("@Text", text, DbType.String, 16)
							.ExecuteList(new Coder天气实况EO().GetMapper(), tm);
		}
		/// <summary>
		/// 按字段查询
		/// </summary>
		/// <param name = "renewTime"></param>
		/// <param name = "top">获取记录数，当top值小于或等于0时忽略top条件</param>
		/// <param name = "sort">排序表达式，如：field1 ASC,field2 DESC</param>
		/// <param name="tm">事务管理对象</param>
		/// <return></return>
		public List<Coder天气实况EO> SelectByRenewTime(DateTime renewTime, int top, string sort, TransactionManager tm = null)
		{
			var sql = new StringBuilder();
			var format = new StringBuilder();
			format.Append(@"SELECT {0} [id], [stationnum], [title], [aws_type], [text], [renewTime] FROM [dbo].[天气实况] WHERE [renewTime]=@RenewTime");
			if (!string.IsNullOrEmpty(sort)) format.AppendFormat(" ORDER BY {0} ", sort);
			sql.AppendFormat(format.ToString(), (top > 0) ? string.Format("TOP {0}", top) : string.Empty);
			return AdoBase(sql.ToString())
							.AddInParameter("@RenewTime", renewTime, DbType.DateTime, 8)
							.ExecuteList(new Coder天气实况EO().GetMapper(), tm);
		}

		/// <summary>
		/// 按自定义条件查询
		/// </summary>
		/// <param name = "where">自定义条件,where子句</param>
		/// <param name = "top">获取行数</param>
		/// <param name = "sort">排序表达式</param>
		/// <param name = "paras">where子句中定义的参数集合</param>
		/// <param name="tm">事务管理对象</param>
		/// <return></return>
		public List<Coder天气实况EO> Select(string where, int top, string sort, IEnumerable<DbParameter> paras, TransactionManager tm = null)
		{
			var sql = new StringBuilder();
			var format = new StringBuilder();
			format.Append(@"SELECT {0} [id], [stationnum], [title], [aws_type], [text], [renewTime] FROM [dbo].[天气实况] ");
			if (!string.IsNullOrEmpty(where)) format.AppendFormat(" WHERE {0} ", where);
			if (!string.IsNullOrEmpty(sort)) format.AppendFormat(" ORDER BY {0} ", sort);
			sql.AppendFormat(format.ToString(), (top > 0) ? string.Format("TOP {0}", top) : string.Empty);
			return AdoBase(sql.ToString())
							.AddParameters(paras)
							.ExecuteList(new Coder天气实况EO().GetMapper(), tm);
		}

		/// <summary>
		/// 分页查询
		/// </summary>
		/// <param name = "where">条件表达式</param>
		/// <param name = "sort">排序表达式</param>
		/// <param name = "paras">传入参数</param>
		/// <param name = "pageSize">页大小</param>
		/// <param name = "pageIndex">页码</param>
		/// <param name = "recordCount">总记录数，由于计算总记录数影响数据库性能，只有小于或等于0的情况下才返回总记录数</param>
		/// <return>分页数据</return>
		public List<Coder天气实况EO> SelectPager(string where, string sort, IList<DbParameter> paras, long pageSize, long pageIndex, ref long recordCount)
		{
			var sql = new StringBuilder();
			sql.AppendFormat(@"SELECT [id], [stationnum], [title], [aws_type], [text], [renewTime] FROM [dbo].[天气实况]");
			if (!string.IsNullOrEmpty(where)) sql.AppendFormat(" WHERE {0} ", where);
			if (!string.IsNullOrEmpty(sort)) sql.AppendFormat(" ORDER BY {0} ", sort);
			var pager = this.DataBase.CreatePager(sql.ToString(), pageSize);
			pager.AddParameters(paras);
			if (recordCount <= 0)
			{
				recordCount = pager.GetRecordCount();
			}
			return pager.GetPageData(pageIndex).MapToList(new Coder天气实况EO().GetMapper());
		}

		#endregion

		#region Insert方法集合
		/// <summary>
		/// 插入数据，有自增列时，item中自增字段为新增值，否则，返回影响的行数
		/// </summary>
		/// <param name="tm">事务管理对象</param>
		/// <param name = "item">要插入的实体对象</param>
		/// <return></return>
		public int Insert(Coder天气实况EO item, TransactionManager tm = null)
		{
			const string sql = @"INSERT INTO [dbo].[天气实况] ([stationnum], [title], [aws_type], [text], [renewTime]) VALUES (@stationnum, @title, @aws_type, @text, @renewTime) ;SELECT SCOPE_IDENTITY();";
			item.Id = long.Parse(AdoBase(sql)
												.AddInParameter("@stationnum", item.Stationnum, DbType.AnsiString, 16)
												.AddInParameter("@title", item.Title != null ?  (object)item.Title : DBNull.Value, DbType.AnsiString, 256)
												.AddInParameter("@aws_type", item.AwsType.HasValue ? (object)item.AwsType.Value : DBNull.Value, DbType.Int16, 2)
												.AddInParameter("@text", item.Text != null ?  (object)item.Text : DBNull.Value, DbType.String, -1)
												.AddInParameter("@renewTime", item.RenewTime, DbType.DateTime, 8)
												.ExecuteScalar(tm).ToString());
			return 1;
		}
		#endregion

		#region Update方法集合
		/// <summary>
		/// 按自定义条件更新数据
		/// </summary>
		/// <param name = "set">更新列</param>
		/// <param name = "where">更新条件</param>
		/// <param name = "paras">参数集合</param>
		/// <param name="tm">事务管理对象</param>
		/// <return></return>
		public int Update(string set, string where, IEnumerable<DbParameter> paras, TransactionManager tm = null)
		{
			if (string.IsNullOrEmpty(set)) throw new ArgumentNullException("set");
			var sql = string.Format(@"UPDATE [dbo].[天气实况] SET {0} {1}", set, (string.IsNullOrEmpty(where) ? string.Empty : " WHERE " + where));
			return AdoBase(sql)
							.AddParameters(paras)
							.ExecuteNonQuery(tm);
		}
		#endregion

		#region Delete方法集合
		/// <summary>
		/// 按字段删除
		/// </summary>
		/// <param name="tm">事务管理对象</param>
		/// <param name = "id"></param>
		public int DeleteById(long id, TransactionManager tm = null)
		{
			const string sql = @"DELETE FROM [dbo].[天气实况] WHERE [id]=@id";
			return AdoBase(sql)
							.AddInParameter("@id", id, DbType.Int64, 8)
							.ExecuteNonQuery(tm);
		}
		/// <summary>
		/// 按字段删除
		/// </summary>
		/// <param name="tm">事务管理对象</param>
		/// <param name = "stationnum"></param>
		public int DeleteByStationnum(string stationnum, TransactionManager tm = null)
		{
			const string sql = @"DELETE FROM [dbo].[天气实况] WHERE [stationnum]=@stationnum";
			return AdoBase(sql)
							.AddInParameter("@stationnum", stationnum, DbType.AnsiString, 16)
							.ExecuteNonQuery(tm);
		}
		/// <summary>
		/// 按字段删除
		/// </summary>
		/// <param name="tm">事务管理对象</param>
		/// <param name = "title"></param>
		public int DeleteByTitle(string title, TransactionManager tm = null)
		{
			const string sql = @"DELETE FROM [dbo].[天气实况] WHERE [title]=@title";
			return AdoBase(sql)
							.AddInParameter("@title", title != null ?  (object)title : DBNull.Value, DbType.AnsiString, 256)
							.ExecuteNonQuery(tm);
		}
		/// <summary>
		/// 按字段删除
		/// </summary>
		/// <param name="tm">事务管理对象</param>
		/// <param name = "awsType"></param>
		public int DeleteByAwsType(short? awsType, TransactionManager tm = null)
		{
			const string sql = @"DELETE FROM [dbo].[天气实况] WHERE [aws_type]=@aws_type";
			return AdoBase(sql)
							.AddInParameter("@aws_type", awsType.HasValue ? (object)awsType.Value : DBNull.Value, DbType.Int16, 2)
							.ExecuteNonQuery(tm);
		}
		/// <summary>
		/// 按字段删除
		/// </summary>
		/// <param name="tm">事务管理对象</param>
		/// <param name = "text"></param>
		public int DeleteByText(string text, TransactionManager tm = null)
		{
			const string sql = @"DELETE FROM [dbo].[天气实况] WHERE [text]=@text";
			return AdoBase(sql)
							.AddInParameter("@text", text != null ?  (object)text : DBNull.Value, DbType.String, -1)
							.ExecuteNonQuery(tm);
		}
		/// <summary>
		/// 按字段删除
		/// </summary>
		/// <param name="tm">事务管理对象</param>
		/// <param name = "renewTime"></param>
		public int DeleteByRenewTime(DateTime renewTime, TransactionManager tm = null)
		{
			const string sql = @"DELETE FROM [dbo].[天气实况] WHERE [renewTime]=@renewTime";
			return AdoBase(sql)
							.AddInParameter("@renewTime", renewTime, DbType.DateTime, 8)
							.ExecuteNonQuery(tm);
		}
		/// <summary>
		/// 删除指定条件的记录
		/// </summary>
		/// <param name = "whereClause">自定义删除条件，where子句。如：id=1 and name='aaa'</param>
		/// <param name = "paras">where子句中定义的参数集合</param>
		/// <param name="tm">事务管理对象</param>
		/// <return></return>
		public int Delete(string whereClause, IEnumerable<DbParameter> paras, TransactionManager tm = null)
		{
			var sql = string.Format(@"DELETE FROM [dbo].[天气实况] {0}", (string.IsNullOrEmpty(whereClause) ? string.Empty : " WHERE " + whereClause));
			return AdoBase(sql)
							.AddParameters(paras)
							.ExecuteNonQuery(tm);
		}
		#endregion

		#region  自定义方法集合
		#endregion

	}


	#endregion
}
